<style type="text/css">
    #hr-scoreMapping-update {
        padding: 20px 25px 25px 0;
    }
</style>

<!-- 注意id属性每个页面id必须独立-->
<div class="layui-fluid" id="hr-scoreMapping-update">
    <!-- 注意lay-filter属性，该属性为form表单唯一属性-->
    <form class="layui-form" action="" lay-filter="hr-scoreMapping-update-form">
        <!-- 隐藏当前对象id-->
        <div class="layui-form-item febs-hide">
            <label class="layui-form-label febs-form-item-require">ID：</label>
            <div class="layui-input-block">
                <input type="text" name="id" data-th-value="${scoreMapping.id}">
            </div>
        </div>

        <!-- 隐藏当前对象id-->
        <div class="layui-form-item febs-hide">
            <label class="layui-form-label febs-form-item-require">积分类型：</label>
            <div class="layui-input-block">
                <input type="text" name="type"  id="hr-scoreMapping-update-type" data-th-value="${scoreMapping.type}">
            </div>
        </div>

        <!-- layui-form-item为自适应长度样式 -->
        <div class="layui-form-item">
            <label class="layui-form-label febs-form-item-require">原始成绩范围最大值</label>
            <div class="layui-input-block">
                <input type="text" name="maxValue"  id="hr-scoreMapping-update-maxValue"  minlength="0" maxlength="32"
                       lay-verify="required|range|name|showMoney" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label febs-form-item-require">原始成绩范围最小值</label>
            <div class="layui-input-block">
                <input type="text" name="minValue"  id="hr-scoreMapping-update-minValue" minlength="0" maxlength="32"
                       lay-verify="required|range|name|showMoney" autocomplete="off" class="layui-input" >
            </div>
        </div>


        <div class="layui-form-item">
            <label class="layui-form-label febs-form-item-require">对应积分值</label>
            <div class="layui-input-block">
                <input type="text" name="integralValue"  id="hr-scoreMapping-update-integralValue" minlength="0" maxlength="32"
                       lay-verify="required|range|name|showMoney" autocomplete="off" class="layui-input">
            </div>
        </div>


        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <textarea name="remark" id="hr-scoreMapping-update-remark" maxlength="100" class="layui-textarea">
                </textarea>
            </div>
        </div>
        <div class="layui-form-item febs-hide">
            <button class="layui-btn" lay-submit="" lay-filter="scoreMapping-update-form-submit"
                    id="hr-scoreMapping-update-submit-btn"></button>
        </div>
    </form>
</div>

<script data-th-inline="javascript">
    layui.use(['febs', 'form', 'validate'], function () {
        let $ = layui.$,
            febs = layui.febs,
            layer = layui.layer,
            form = layui.form,
            // 获得后台出入的实体对象
            scoreMapping = [[${scoreMapping}]],
            // 验证
            validate = layui.validate;
        // 表单验证
        form.verify(validate);
        // 表单渲染
        form.render();
        // 初始化页面数据
        initScoreMappingValue();
        // 初始化页面数据
        function initScoreMappingValue() {
            form.val("hr-scoreMapping-update-form", {
                "maxValue": scoreMapping.maxValue,
                "minValue": scoreMapping.minValue,
                "integralValue": scoreMapping.integralValue,
                "remark":scoreMapping.remark
            });
        }

        //原始成绩最大值，最小值及对应积分值的change事件，实现remark的自动修改

        function setRemark() {
            var type = $("#hr-scoreMapping-update-type").val();
            var maxValue = $("#hr-scoreMapping-update-maxValue").val();
            var minValue = $('#hr-scoreMapping-update-minValue').val();
            var integralValue = $("#hr-scoreMapping-update-integralValue").val();
            if (type==1){
                $("#hr-scoreMapping-update-remark").val('每' + minValue + '-' + maxValue + '课时,对应' + integralValue + '积分');
            }
            else{
                $("#hr-scoreMapping-update-remark").val('考试' + minValue + '-' + maxValue + '分,对应' + integralValue + '积分');
            }
        }


            //输入框change事件

            setRemark();
            $("#hr-scoreMapping-update-maxValue").change(function () {
                setRemark();
            });

            $("#hr-scoreMapping-update-minValue").change(function () {
                setRemark();
            });

            $("#hr-scoreMapping-update-integralValue").change(function () {
                setRemark();
            });

        // 表单提交事件

        form.on('submit(scoreMapping-update-form-submit)', function (data) {
            console.log("进入修改" + data.field)
            // 如果都没有修改直接返回
            if (febs.nativeEqual(data.field, scoreMapping)) {
                febs.alert.warn('数据未作任何修改！');
                return false;
                // 如果只修改了最大分数之外的东西，则直接更新
            }else if(febs.nativeEqual(data.field.maxValue, scoreMapping.maxValue)
                &&febs.nativeEqual(data.field.minValue, scoreMapping.minValue)){
                febs.post(ctx + 'scoreMapping/update', data.field, function () {
                    layer.closeAll();
                    febs.alert.success('积分规则修改成功');
                    // 点击父页面查询按钮
                    $('#hr-scoreMapping').find('#hr-scoreMapping-query-btn').click();
                });
                // 如果分数或者课时发生变化先进行判断是否存在重复的，如果重复则返回，不重复则修改
            }else {
                let maxValue = data.field.maxValue;
                let minValue = data.field.minValue;
                if(febs.nativeEqual(data.field.maxValue, scoreMapping.maxValue)){
                    maxValue = null;
                }
                if(febs.nativeEqual(data.field.minValue, scoreMapping.minValue)) {
                    minValue = null;
                }

                $.ajax({
                    type: 'get',
                    url: 'scoreMapping/isRepetitive',
                    data: {"maxValue": maxValue, "minValue": minValue},
                    success:function(res){
                        if(res){
                            febs.post(ctx + 'scoreMapping/update', data.field, function () {
                                layer.closeAll();
                                febs.alert.success('积分规则修改成功');
                                $('#hr-scoreMapping').find('#hr-scoreMapping-query-btn').click();
                            });
                        }else{
                            febs.alert.error("输入的分数重复，请检查");
                        }
                    }
                });
            }
            return false;
        });
    });
</script>